# suport vim modul
setw -g mode-keys vi
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
run-shell ~/.tmux/tmux-resurrect/resurrect.tmux

# Changes to this file aren't read by tmux automatically,
# compleletely close the tmux sesssion and reopen it or
# ~PREFIX :~ to open command mode and type "source-file ~/.tmux.conf" or
# "tsr" in terminal which is an alias in ~/.fishrc or use binding `M-c r` to make it work
#
# from FAQ file in tmux source code:
# to see the default session options by starting a new tmux server with no configuration file:
# 	$ tmux -Lfoo -f/dev/null start\; show -g
# Or the default window options:
# 	$ tmux -Lfoo -f/dev/null start\; show -gw

# solve the arrows mess when using vim in tmux
set -g default-terminal "xterm-256color"

# use mouse to scroll the output page instead of the command history
set -g terminal-overrides 'xterm*:smcup@:rmcup@'

# set the prefix from C-b to M-c
set -g prefix M-c
# reload the file using Prefix-r
bind r source-file ~/.tmux.conf \; display "~/.tmux.conf reloaded!"

# switch pane, default is with -r (repeated without prefix)
bind Up select-pane -U
bind k select-pane -U
bind Down select-pane -D
bind j select-pane -D
bind Left select-pane -L
bind h select-pane -L
bind Right select-pane -R
bind l select-pane -R

# use M-c M-c to switch to last window
bind M-c last-window
# M-c t to kill-pane
bind -r t kill-pane
# kill all other panes but the current one
bind T confirm-before "kill-pane -a"
# kill current window
#bind -r x kill-window
# kill all the other windows but the current one
bind X confirm-before "kill-window -a"

# Start Window Numbering at 1
set -g base-index 1
setw -g pane-base-index 1

# Aggressive resize(don't know what this means)
setw -g aggressive-resize on

# prevoius/next-window
bind -r M-x previous-window
bind -r M-v next-window
# or
# bind -r M-x select-window -t :-
# bind -r M-v select-window -t :+

# Vertial split-window from current dir
bind -r v split-window -h -c "#{pane_current_path}"
bind -r \ split-window -h -c "#{pane_current_path}"
# Horizontal split-window from current dir
bind -r h split-window -c "#{pane_current_path}"
bind -r - split-window -c "#{pane_current_path}"
# create new window #from the current dir
bind c new-window # -c "#{pane_current_path}"

# copy the text in terminal using the following method:(not need to go into the copy mode first)
# hold Shift and using touchpad/mouse tmux kill-pane -aselect the text you want
# press C-S-c at the same time to copy, the text will be filled into the X clipboard
# C-S-v to paste


# use prefix+z to max/restore the current pane
# prefix+M-z to max the other one, unzoomed the window if it is already zoomed
bind M-z if-shell "[ #{window_zoomed_flag} -eq 1 ]" "resize-pane -Z" "last-pane \; resize-pane -Z"

# copy/paste between system clipboard and tmux
set -g mouse on
# toggle mouse on/off
bind m set -g mouse
# prefix+[ first, select the content with mouse(hold) and then copy using M-w,
# paste using C-y, using prefix+[ will also solve the problem that
# selecting multiple lines with mouse selects contents cross panes.
# To copy:
bind-key -n -t emacs-copy M-w copy-pipe "xclip -i -sel p -f | xclip -i -sel c "
# To paste:
bind-key -n C-y run "xclip -o | tmux load-buffer - ; tmux paste-buffer"

# Use vi-style navigation in Copy mode (which is also scrollback mode)
#setw -g mode-keys vi

# scrollback limit
#set-option -g history-limit 30000
set -g history-limit 30000

# Lower escape timing from 500ms to 50ms for quicker response to scroll-buffer access.
set -s escape-time 50

# Reduce the command delay time to something a bit shorter
#set -sg escape-time 1
# Extend the repeat interval for repeatable commands (e.g., resize-pane)
#set -sg repeat-time 500

# how long to show pane numbers when pressing M-c q
set -g display-panes-time 2000  # (which is 2sec)

# Automatically set window title to reflect the current program
setw -g automatic-rename on
# setw -g automatic-rename-format '{#T}'

# renumber the windows if any of them is closed
#set -g renumber-windows on

# disable M-c C-z to suspend tmux
unbind-key C-z
# disable C-d to exit shell, this C-d happens when being first input to prompt
unbind-key C-d

# clear screen
#bind -n C-l send-keys 'C-l'
#unbind-key C-l
#bind -n C-l send-keys 'C-l'

# network speed in status line, ~/.local/bin/net-speed.sh
# set update frequencey (default 15 seconds)
set -g status-interval 1
#set -g status-left '#(net-speed.sh wlp5s0)#[default]'
# Set status bar
set -g status-bg default # default->transparent
set -g status-fg default # green
# set -g status-left "[#S#I#P:#{pane_height}x#{pane_width}] "
set -g status-left " #S [#I:#P] : "
# check `man shrftime` for the meaning of %... and more
# it seems the pane_current_path is not necessary
#set -g status-right "\"#{=50:pane_current_path}\" %H:%M %a %d-%b"
#set -g status-right "%H:%M %a %m/%d"
set -g status-right '#(tmux display-message -p "#{=50:pane_current_path}" | sed "s#$HOME#~#g") [weekNo.%U] [#{pane_height}x#{pane_width}]'
set -g status-right-length 200
set -g status-left-length 20
#set -g status-utf8 on

# Highlight active window
setw -g window-status-current-fg red
set -g pane-active-border-fg red
# indentify activity in other windows
setw -g monitor-activity on
set -g visual-activity on
setw -g window-status-current-attr bright
setw -g window-status-activity-attr bold,blink,underscore
setw -g window-status-format "[#I#([ #{window_panes} -gt 1 ] && echo :#{window_panes})]:#{pane_current_command}#F"
bind s list-panes -a -F '#S:#I:#P-#{pane_height}x#{pane_width} "#{pane_title}" #{history_size}'

#bind w choose-tree -u
bind w choose-tree -u -W '#{window_index}(#{window_panes}): #{window_name} #{session_attached,yes,} #{pane_current_path} #F'

set -g message-fg red
set -g message-bg default
set -g message-attr bright
set -g display-time 3000

# scroll the screen of the other pane from the current pane
bind -r M-PageDown last-pane \; send-keys "PageDown" \; last-pane
bind -r M-PageUp last-pane \; send-keys "PageUp" \; last-pane
bind -r M-Home last-pane \; send-keys "Up" \; last-pane
bind -r M-End last-pane \; send-keys "Down" \; last-pane
